//
//  MSMationController.m
//  觅尚
//
//  Created by No.8 on 2019/3/19.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSMationController.h"
#import "MSMationCell.h"
#import "MSMationModel.h"
#import "MSMationURLController.h"
#import "HWRollingView.h"
#define pageSize 5


@interface MSMationController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger page;
    NSInteger count;
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;


/** 轮播图*/
@property(nonatomic,strong)UIImageView * scrollIMG;


/** 数组*/
@property(nonatomic,strong)NSMutableArray *array;

/** 数组*/
@property(nonatomic,strong)NSMutableArray *bannerarray;

/** 数组*/
@property(nonatomic,strong)NSMutableArray *banneModelrarray;

/** HeaderView*/
@property(nonatomic,strong)UIView *HeaderView;

/** HeaderView*/
@property(nonatomic,strong)UIView *Navtitle;

@end

@implementation MSMationController
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
        header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    
}
    - (void)MJChiBaoZiHeaderRefresh{
        page = 1;
        [self ReloadData];

    }
- (void)MJChiBaoZiFootderRefresh{
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    [self FootderRefreshReloadData];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray new];
    self.bannerarray = [NSMutableArray new];
    
    self.banneModelrarray = [NSMutableArray new];
    self.view.backgroundColor = backgroundcolor;
    
    [self CreattableView];
    
    [self CreatView];
    
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.tableView.mj_footer = footder;
    
}
- (void)ReloadData{
    [self.array removeAllObjects];
    [self.bannerarray removeAllObjects];
    
    NSDictionary * params = @{@"page":@(page),@"pageSize":@(pageSize)};
    [HTTPRequest POST:MSgetActivity params:params succeed:^(id responseObject) {
//        WYCLog(@"----------%@",responseObject);
        WYCLog(@"----------%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_header endRefreshing];
            count = [responseObject[@"data"][@"count"] integerValue];
            [self.scrollIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_Hot,responseObject[@"activityImgRoute"]]] placeholderImage:[UIImage imageNamed:@"huodongtupian"]];
            NSMutableArray *temporaryArray = [NSMutableArray new];
            
            NSMutableArray *tembannerArray = [NSMutableArray new];
            
            
            NSMutableArray *banneModeArray = [NSMutableArray new];
            for (NSDictionary * dic in responseObject[@"data"][@"activityList"]) {
                
                    MSMationModel * model = [MSMationModel mj_objectWithKeyValues:dic];
                    [temporaryArray addObject:model];
                self.array = temporaryArray;
//                    WYCLog(@"------%@",model.winningId);
            }
            
            for (NSDictionary * dic in responseObject[@"data"][@"banner"]) {
                MSMationModel * model = [MSMationModel mj_objectWithKeyValues:dic];
                [tembannerArray addObject:model.activityImgRoute];
                self.bannerarray = tembannerArray;
                
                [banneModeArray addObject:model];
                self.banneModelrarray = banneModeArray;
            }
            
            
        }else{
            [self.tableView.mj_header endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page = [params[@"page"] integerValue];
        }
        [self setbanner];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)FootderRefreshReloadData{
    [self.array removeAllObjects];
    [self.bannerarray removeAllObjects];
    
    NSDictionary * params = @{@"page":@(page),@"pageSize":@(pageSize)};
    [HTTPRequest POST:MSgetActivity params:params succeed:^(id responseObject) {
        //        WYCLog(@"----------%@",responseObject);
        WYCLog(@"----------%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_footer endRefreshing];
            [self.scrollIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_Hot,responseObject[@"activityImgRoute"]]] placeholderImage:[UIImage imageNamed:@"huodongtupian"]];
            
            for (NSDictionary * dic in responseObject[@"data"][@"activityList"]) {
                
                MSMationModel * model = [MSMationModel mj_objectWithKeyValues:dic];
                [self.array addObject:model];
                //                    WYCLog(@"------%@",model.winningId);
            }
            
            for (NSDictionary * dic in responseObject[@"data"][@"banner"]) {
                MSMationModel * model = [MSMationModel mj_objectWithKeyValues:dic];
                [self.bannerarray addObject:model.activityImgRoute];
                [self.banneModelrarray addObject:model];
            }
            
            
        }else{
            [self.tableView.mj_footer endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page --;
        }
        [self setbanner];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
    
}
- (void)CreatView{
    
    
    UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 850 * px)];
    HeaderView.backgroundColor = backgroundcolor;
    [self.view addSubview:HeaderView];
    self.HeaderView = HeaderView;
    
    UIView * ImageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 400 * px)];
    UIImage*image = [UIImage imageNamed:@"hdzx_bg"];
    ImageView.layer.contents = (__bridge id)image.CGImage;
    /** 设置对齐模式*/
    ImageView.layer.contentsGravity = kCAGravityResize;
    [HeaderView addSubview:ImageView];
    
    
    
    
    /** title*/
    UILabel * Navtitle = [[UILabel alloc] init];
    if ([WRNavigationBar isIphoneX]) {
        Navtitle.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 55, 300 * px, 60 * px);
    }else{
        Navtitle.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 30, 300 * px, 60 * px);
        
    }
    Navtitle.textColor = [UIColor whiteColor];
    Navtitle.text = @"活动资讯";
    Navtitle.textAlignment = NSTextAlignmentCenter;
    Navtitle.font = [UIFont boldSystemFontOfSize:SYRealValue(38 / 2)];
    [HeaderView addSubview:Navtitle];
    self.Navtitle = Navtitle;
    
    /** 日历按钮*/
//    UIImageView * scrollIMG = [[UIImageView alloc] init];
////    scrollIMG.contentMode = UIViewContentModeScaleAspectFill;
//    scrollIMG.frame = CGRectMake(30 * px, CGRectGetMaxY(Navtitle.frame) + 30 * px, ScreenWidth - 69 * px, 500 * px);
//    scrollIMG.image = [UIImage imageNamed:@"huodongtupian"];
//    [HeaderView addSubview:scrollIMG];
//    self.scrollIMG = scrollIMG;
    
    
    /** 标题图标*/
    UIImageView * WinningIMG = [[UIImageView alloc] initWithFrame:CGRectMake(30 * px, CGRectGetMaxY(Navtitle.frame) + 550 * px, 60 * px, 60 * px)];
    WinningIMG.image = [UIImage imageNamed:@"zjxx_icon"];
    [HeaderView addSubview:WinningIMG];
    
    
    
    /** 中奖信息*/
    UILabel * WinningLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(WinningIMG.frame) + 20 * px, WinningIMG.y, 200 * px, 60 * px)];
    WinningLabel.textColor = [UIColor whiteColor];
    WinningLabel.text = @"资讯";
    WinningLabel.textAlignment = NSTextAlignmentLeft;
    WinningLabel.font = [UIFont boldSystemFontOfSize:SYRealValue(30 / 2)];
    [HeaderView addSubview:WinningLabel];
    
    
    UIView *LineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(WinningLabel.frame) + 30 * px, ScreenWidth, 1)];
    LineView.backgroundColor = BackViewColor;
    [HeaderView addSubview:LineView];
    
    
//    self.tableView.tableHeaderView = HeaderView;
    
}

//轮播图
- (void)setbanner{
    
    HWRollingView * RollingView = [HWRollingView rollingViewWithFrame:CGRectMake(30 * px, CGRectGetMaxY(_Navtitle.frame) + 30 * px, ScreenWidth - 60 * px, 500 * px) RollingStyle:RollingStyleNetwork Data:self.bannerarray action:^(NSInteger index) {
                    NSLog(@"点击了第%ld页", index);
        MSMationModel * model = self.banneModelrarray[index];
        NSLog(@"点击了第%@页", model.activityUrl);
        MSMationURLController *MationUrlVC = [[MSMationURLController alloc] init];
        
        WYCLog(@"--------%@",model.activityUrl);
        MationUrlVC.UrlString = model.activityUrl;
        [self.navigationController pushViewController:MationUrlVC animated:MationUrlVC];
        //        weakSelf.titleLabel.text = [NSString stringWithFormat:@"点击了第%ld页", index];
    }];
    RollingView.layer.cornerRadius = 20 * px;
    RollingView.layer.masksToBounds = YES;
    [self.HeaderView addSubview:RollingView];
}
/** 表视图*/
- (void)CreattableView{
//    self.extendedLayoutIncludesOpaqueBars = YES;
    
        
        if (@available(iOS 11.0, *)) {
            
            UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 820 * px, ScreenWidth, ScreenHeight - 820 * px - [self navBarBottom]) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        self.tableView.separatorColor = BackViewColor;
    
        [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 350 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSMationCell * cell = [MSMationCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.model = self.array[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MSMationModel * model = self.array[indexPath.row];
    MSMationURLController *MationUrlVC = [[MSMationURLController alloc] init];
    
    WYCLog(@"--------%@",model.activityUrl);
    MationUrlVC.UrlString = model.activityUrl;
    [self.navigationController pushViewController:MationUrlVC animated:MationUrlVC];
    
}

- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
